﻿//  Author:
//       Daniel Großer <olav.daniel.grosser@googlemail.com>
//  
//  Copyright (c) 2012 Copyright (C) 2012 by Daniel Großer. This Code is under GNU LGPL 3.0
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.
// 
//  You should have received a copy of the GNU Lesser General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Beauty;
using Beauty.Tools;
using Beauty.ErrorHandling;

namespace Beauty.Test
{
    /// <summary>
    /// This class is for testing the behavior of Beauty on invalid inputs of
    /// either code or grammar.
    /// </summary>
    [TestFixture]
    public class Errors
    {
        [ExpectedException(typeof(ParserException))]
        [Test]
        public void Provoke_Expected_EOF()
        {
            var parser = new Parser(new NamespaceLanguageProvider(GetType().Assembly, "Beauty.Test.Languages.Language1"));

            parser.Parse<Languages.Language1.BeginKeyword>("begin more code");
        }

        [ExpectedException(typeof(ParserException))]
        [Test]
        public void Provoke_MissingConstructor()
        {
            new Parser(new NamespaceLanguageProvider(GetType().Assembly, "Beauty.Test.Languages.Language2"));
        }

        [ExpectedException(typeof(ParserException))]
        [Test]
        public void Provoke_ExpectedTerminalsNotUnique()
        {
            new Parser(new NamespaceLanguageProvider(GetType().Assembly, "Beauty.Test.Languages.Language3"));
        }

        [ExpectedException(typeof(ParserException))]
        [Test]
        public void Provoke_AlternationNotUnique()
        {
            new Parser(new NamespaceLanguageProvider(GetType().Assembly, "Beauty.Test.Languages.Language4"));
        }

        [ExpectedException(typeof(ParserException))]
        [Test]
        public void Provoke_LeftRecursionDetected()
        {
            new Parser(new NamespaceLanguageProvider(GetType().Assembly, "Beauty.Test.Languages.Language5"));
        }

        [ExpectedException(typeof(ParserException))]
        [Test]
        public void Provoke_NoExpectedTerminals()
        {
            new Parser(new NamespaceLanguageProvider(GetType().Assembly, "Beauty.Test.Languages.Language6"));
        }

        [ExpectedException(typeof(ParserException))]
        [Test]
        public void Provoke_AST_NotInLanguage()
        {
            new Parser(new NamespaceLanguageProvider(GetType().Assembly, "Beauty.Test.Languages.Language7"));
        }

        [ExpectedException(typeof(ParserException))]
        [Test]
        public void Provoke_EpsilonDetected()
        {
            new Parser(new NamespaceLanguageProvider(GetType().Assembly, "Beauty.Test.Languages.Language8"));
        }

        [ExpectedException(typeof(ParserException))]
        [Test]
        public void Provoke_SyntaxError()
        {
            var parser = new Parser(new NamespaceLanguageProvider(GetType().Assembly, "Beauty.Test.Languages.Language1"));

            parser.Parse<Languages.Language1.Block>("declare end");
        }

        /// <summary>
        /// Check that this test class tests all error types.
        /// </summary>
        [Test]
        public void Completeness()
        {
            foreach (var errorType in Enum.GetNames(typeof(ParserErrorType)))
                Assert.IsNotNull(GetType().GetMethod(string.Format("Provoke_{0}", errorType)), string.Format("Beauty needs a unit test for ParserErrorType {0}.", errorType));
        }
    }
}
